import playscore.board;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*; // 导入所有 util 类（包括 List 和 ArrayList）


class GUI extends JFrame {
    private playscore.board board = new board();
    private int rightanswers = 0;
    private int questionnum = 0;
    private int num1;
    private int num2;
    private char fuhao;

    private JLabel question;
    private JTextField answer;
    private JLabel result;
    private JTextArea boardarea;

    public GUI() {
        setTitle("算术运算测试");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 500);

        board.loadboard();

        JPanel mainpane = new JPanel();//主面板
        mainpane.setLayout(new GridLayout(4, 1));

        question = new JLabel();
        mainpane.add(question);

        answer = new JTextField();
        mainpane.add(answer);

        JButton button = new JButton("提交答案");
        button.addActionListener(new buttonlistener());
        mainpane.add(button);

        result = new JLabel();
        mainpane.add(result);

        boardarea = new JTextArea();
        boardarea.setEditable(false);
        board.updateboardArea(boardarea);

        add(mainpane, BorderLayout.CENTER);//将主面板添加到窗口的中心区域
        add(new JScrollPane(boardarea), BorderLayout.SOUTH);//将可滚动的 boardArea 添加到窗口的南部区域。

        generateQuestion();
        setVisible(true);
    }

    private void generateQuestion() {
        if (questionnum < 10) {
            Random random = new Random();
            num1 = random.nextInt(100);
            num2 = random.nextInt(100);
            fuhao = Randomoperator();

            question.setText(String.format("第 %d 题: %d %c %d = ", questionnum + 1, num1, fuhao, num2));
        } else {
            int score = rightanswers * 10;
            String playerName = JOptionPane.showInputDialog(this, "请输入你的姓名:");
            board.addPlayerScore(playerName, score);
            board.saveboard();
            board.updateboardArea(boardarea);
            JOptionPane.showMessageDialog(this, "你的得分是: " + score);
            System.exit(0);
        }
    }

    private char Randomoperator()
    {
        Random random = new Random();
        return random.nextInt(2) == 0? '+' : '-';
    }

    private int calculateAnswer(int num1, int num2, char fuhao)
    {
        return fuhao == '+'? num1 + num2 : num1 - num2;
    }

    private class buttonlistener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            try {
                int playerAnswer = Integer.parseInt(answer.getText().trim());
                int correctAnswer = calculateAnswer(num1, num2, fuhao);

                if (playerAnswer == correctAnswer) {
                    rightanswers++;
                    result.setText("你答对了");
                } else {
                    result.setText("你答错了，正确答案是: " + correctAnswer);
                }

                questionnum++;
                generateQuestion();
                answer.setText("");
            } catch (NumberFormatException ex) {
                result.setText("请输入有效的数字答案。");
                answer.setText("");
            }
        }
    }

    public static void main(String[] args)
    {
        new GUI();
    }
}